package bjx.com.kehu.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import bjx.com.kehu.R;
import bjx.com.kehu.utils.SPUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/11/16.
 */

public class UpdateUserZFBActivity extends BaseActivity {

    ImageView mIVBack;
    TextView mTVTitle;
    TextView mTVEvent;

    EditText mETZFB;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_udtzfb);
        mIVBack = getViewById(R.id.back);
        mTVTitle = getViewById(R.id.title);
        mTVTitle.setText("修改支付宝");
        mTVEvent = getViewById(R.id.event);
        mTVEvent.setText("保存");
        mTVEvent.setVisibility(View.VISIBLE);

        mETZFB = getViewById(R.id.udtzfb_et_zfb);
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
        mTVEvent.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.event:
                final String zfb = mETZFB.getText().toString().trim();
                mEngine.updateUserInfo("", zfb, userModel.getUser_id()).enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String str = response.body().string();
                            JSONObject jo = new JSONObject(str);
                            int status = jo.getInt("status");
                            String msg = jo.getString("msg");
                            if (status == 200) {
                                showToast("修改成功");
                                userModel.setAlipay(zfb);
                                SPUtils.saveObject(mApp, "usermodel", userModel);
                                finish();
                            } else if (status == 201) {
                                showToast("用户不存在");
                            } else if (status == 100) {
                                showToast("请求失败");
                            } else {
                                showToast("请求异常");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                    }
                });
                break;
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }
}
